package com.hzhanghuan.mdsearchview.app;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.hzhanghuan.materialsearchview.MaterialSearchView;
import com.hzhanghuan.materialsearchview.SearchListener;
import com.hzhanghuan.materialsearchview.Suggestion;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


/**
 * Created by Administrator on 2016/7/6.
 */
public class SearchActivity extends AppCompatActivity {

    private MaterialSearchView searchView;

    private Toolbar toolbar;





    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        searchView = (MaterialSearchView) findViewById(R.id.search_view);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        searchView.setSearchBarBackgroundColorRes(android.R.color.black);
        searchView.setSearchTextColorRes(android.R.color.white);
        searchView.setBackTintColor(Color.WHITE);
        searchView.setSearchListener(new SearchListener() {
            @Override
            public boolean onSearch(MaterialSearchView view,String search) {
                Toast.makeText(getApplicationContext(), search, Toast.LENGTH_SHORT).show();
                return true;
            }





            @Override
            public void onEdit(MaterialSearchView view,String edit) {
                if(TextUtils.isEmpty(edit)){
                    view.updateSuggestions(Collections.<Suggestion>emptyList());
                    return;
                }
                List<Suggestion> suggestions=new ArrayList<>();
                suggestions.add(Suggestion.createSuggestion(edit,new Intent(SearchActivity.this,SearchActivity.class)));
                suggestions.add(Suggestion.createSuggestion(edit,new Intent(SearchActivity.this,SearchActivity.class)));
                suggestions.add(Suggestion.createSuggestion(edit,new Intent(SearchActivity.this,SearchActivity.class)));
                suggestions.add(Suggestion.createSuggestion(edit,new Intent(SearchActivity.this,SearchActivity.class)));
                suggestions.add(Suggestion.createSuggestion(edit,new Intent(SearchActivity.this,SearchActivity.class)));
                view.updateSuggestions(suggestions);
            }
        });
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search, menu);
        return true;
    }





    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search: {
                searchView.openSearch();
                return true;
            }
        }
        return super.onContextItemSelected(item);
    }
}
